﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;

namespace Footstep.CreateFootstep
{
	public partial class CreateFootstepForm : System.Windows.Forms.Form
	{
		public CreateFootstepForm(List<Level> levels, List<double> levelElevations, List<Material> materials, ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_Initialized = false;
			this.InitializeComponent();
			this.m_Levels = levels;
			this.m_Levels.Reverse();
			this.m_LevelElevations = levelElevations;
			this.m_materials = materials;
			this.m_LevelElevations.Reverse();
			this.InitializeInfo();
			this.m_Initialized = true;
			ToolTip toolTip = new ToolTip();
			toolTip.SetToolTip(this.btnRect1, "创建单边矩形台阶");
			ToolTip toolTip2 = new ToolTip();
			toolTip2.SetToolTip(this.btnRect2, "创建双边矩形台阶");
			ToolTip toolTip3 = new ToolTip();
			toolTip3.SetToolTip(this.btnRect3, "创建三边矩形台阶");
			ToolTip toolTip4 = new ToolTip();
			toolTip4.SetToolTip(this.btnBow, "创建弓形台阶");
			ToolTip toolTip5 = new ToolTip();
			toolTip5.SetToolTip(this.btnFreedom, "创建自由边台阶");
			ToolTip toolTip6 = new ToolTip();
			toolTip6.SetToolTip(this.btnCancel, "退出");
		}

		protected void InitializeInfo()
		{
			foreach (Level level in this.m_Levels)
			{
				this.cmbTopLevel.Items.Add(level.Name);
				this.cmbBottomLevel.Items.Add(level.Name);
			}
			if (this.m_Levels.Count > 1)
			{
				this.cmbTopLevel.SelectedIndex = 1;
				this.cmbBottomLevel.SelectedIndex = 0;
				double currentElevation = this.m_Revit.Application.ActiveUIDocument.ActiveView.GenLevel.Elevation;
				currentElevation = AssistFunc.feetToMM(currentElevation) / 1000.0;
				int num = this.m_LevelElevations.FindIndex((double s) => Geometry.IsEqual(currentElevation, s));
				int num2;
				int num3;
				if (num == -1)
				{
					num2 = 0;
					num3 = this.m_LevelElevations.Count - 1;
				}
				else
				{
					num2 = num;
					if (num == 0)
					{
						num3 = num2;
					}
					else
					{
						num3 = num - 1;
					}
				}
				this.cmbTopLevel.SelectedIndex = num3;
				this.cmbBottomLevel.SelectedIndex = num2;
				this.tbTopElevation.Text = this.GetElevationByLevelIndex(num3);
				this.tbBottomElevation.Text = this.GetElevationByLevelIndex(num2);
			}
			this.nudStepCount.TextChanged += this.nudStepCount_TextChanged;
			this.cmbMaterial.Items.Clear();
			foreach (Material material in this.m_materials)
			{
				this.cmbMaterial.Items.Add(material.Name);
			}
			this.cmbMaterial.SelectedIndex = 0;
		}

		public CreateFootstepForm.ResultType theResultType
		{
			get
			{
				return this.m_ResultType;
			}
		}

		public bool IsInputPlatformWidth
		{
			get
			{
				return this.rdbInput.Checked;
			}
		}

		public double PlatformWidth
		{
			get
			{
				string text = this.tbPlatformWidth.Text;
				return AssistFunc.mmToFeet(Convert.ToDouble(text));
			}
		}

		public Level BottomLevel
		{
			get
			{
				return this.m_Levels[this.cmbBottomLevel.SelectedIndex];
			}
		}

		public Material FootMaterial
		{
			get
			{
				return this.m_materials[this.cmbMaterial.SelectedIndex];
			}
		}

		public double Offset
		{
			get
			{
				double elevation = this.BottomLevel.Elevation;
				string text = this.tbBottomElevation.Text;
				double num = AssistFunc.mmToFeet(Convert.ToDouble(text) * 1000.0);
				return num - elevation;
			}
		}

		public double StepWidth
		{
			get
			{
				string text = this.tbStepWidth.Text;
				return AssistFunc.mmToFeet(Convert.ToDouble(text));
			}
		}

		public double StepHeight
		{
			get
			{
				string text = this.tbStepHeight.Text;
				return AssistFunc.mmToFeet(Convert.ToDouble(text));
			}
		}

		public int StepCount
		{
			get
			{
				string value = this.nudStepCount.Value.ToString();
				return Convert.ToInt32(value);
			}
		}

		private void tbTopElevation_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.tbTopElevation.Text);
				if (!this.CalculateStepHeight())
				{
					MessageBox.Show("顶部高度入值小于底部高度值，输入无效。", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("顶标高输入值无效.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbBottomElevation_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double.Parse(this.tbBottomElevation.Text);
				if (!this.CalculateStepHeight())
				{
					MessageBox.Show("底部高度值大于顶部高度值，输入无效。", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("底标高输入值无效.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbPlatformWidth_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double val = double.Parse(this.tbPlatformWidth.Text);
				if (Geometry.Lessthan_Or_Equal(val, 0.0))
				{
					MessageBox.Show("底部平台宽度输入值无效.", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("底部平台宽度输入值无效.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void tbStepWidth_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				double val = double.Parse(this.tbStepWidth.Text);
				if (Geometry.Lessthan_Or_Equal(val, 0.0))
				{
					MessageBox.Show("踏步宽度输入值无效.", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("踏步宽度输入值无效.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void nudStepCount_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = int.Parse(this.nudStepCount.Value.ToString());
				if (num <= 0)
				{
					MessageBox.Show("踏步数输入值无效.", "盈建科提示");
					e.Cancel = true;
				}
				else
				{
					this.CalculateStepHeight();
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("踏步数输入值无效.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void cmbTopLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			int selectedIndex = this.cmbTopLevel.SelectedIndex;
			this.tbTopElevation.Text = this.GetElevationByLevelIndex(selectedIndex);
		}

		private void cmbBottomLevel_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.m_Initialized)
			{
				return;
			}
			int selectedIndex = this.cmbBottomLevel.SelectedIndex;
			this.tbBottomElevation.Text = this.GetElevationByLevelIndex(selectedIndex);
		}

		private void btnRect1_Click(object sender, EventArgs e)
		{
			this.m_ResultType = CreateFootstepForm.ResultType.RT_Rect1;
		}

		private void btnRect2_Click(object sender, EventArgs e)
		{
			this.m_ResultType = CreateFootstepForm.ResultType.RT_Rect2;
		}

		private void btnRect3_Click(object sender, EventArgs e)
		{
			this.m_ResultType = CreateFootstepForm.ResultType.RT_Rect3;
		}

		private void btnBow_Click(object sender, EventArgs e)
		{
			this.m_ResultType = CreateFootstepForm.ResultType.RT_Bow;
		}

		private void btnFreedom_Click(object sender, EventArgs e)
		{
			this.m_ResultType = CreateFootstepForm.ResultType.RT_Freedom;
		}

		private void rdbInput_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbInput.Checked)
			{
				this.tbPlatformWidth.Enabled = true;
				return;
			}
			this.tbPlatformWidth.Enabled = false;
		}

		private void nudStepCount_ValueChanged(object sender, EventArgs e)
		{
			this.CalculateStepHeight();
		}

		private void tbTopElevation_TextChanged(object sender, EventArgs e)
		{
			this.CalculateStepHeight();
		}

		private void tbBottomElevation_TextChanged(object sender, EventArgs e)
		{
			this.CalculateStepHeight();
		}

		private void nudStepCount_TextChanged(object sender, EventArgs e)
		{
			this.CalculateStepHeight();
		}

		private void CreateFootstepForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void CreateFootstepForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			string text = this.tbBottomElevation.Text;
			double val = AssistFunc.mmToFeet(Convert.ToDouble(text) * 1000.0);
			string text2 = this.tbTopElevation.Text;
			double val2 = AssistFunc.mmToFeet(Convert.ToDouble(text2) * 1000.0);
			if (Geometry.LessThan(val2, val))
			{
				MessageBox.Show("底标高大于顶标高，无法创建.", "盈建科提示");
			}
			else if (Geometry.IsEqual(val2, val))
			{
				MessageBox.Show("底标高等于顶标高，无法创建.", "盈建科提示");
			}
			this.SaveCompornentData();
		}

		protected string GetElevationByLevelIndex(int index)
		{
			double num = this.m_LevelElevations[index];
			num = (double)Common.GetInt(num * 10000.0);
			return (num / 10000.0).ToString("f3");
		}

		private bool CalculateStepHeight()
		{
			double num = 0.0;
			double num2 = 0.0;
			try
			{
				num = Convert.ToDouble(this.tbTopElevation.Text) * 1000.0;
				num2 = Convert.ToDouble(this.tbBottomElevation.Text) * 1000.0;
			}
			catch (Exception)
			{
				return false;
			}
			double num3 = num - num2;
			if (Geometry.Lessthan_Or_Equal(num3, 0.0))
			{
				return false;
			}
			this.tbStepHeight.Text = (num3 / (double)this.StepCount).ToString("f2");
			return true;
		}

		private void LoadCompornentData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateFootstepForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.nudStepCount.Value = 10m;
				this.tbPlatformWidth.Text = "5000";
				this.tbStepWidth.Text = "300";
				this.CalculateStepHeight();
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.rdbInput.Checked = Convert.ToBoolean(streamReader.ReadLine());
			if (!this.rdbInput.Checked)
			{
				this.rdbPickPoint.Checked = true;
			}
			else
			{
				this.rdbPickPoint.Checked = false;
			}
			this.tbPlatformWidth.Text = streamReader.ReadLine();
			this.nudStepCount.Value = Convert.ToInt32(streamReader.ReadLine());
			this.tbStepWidth.Text = streamReader.ReadLine();
			this.tbTopElevation.Text = streamReader.ReadLine();
			if (this.tbTopElevation.Text.CompareTo("") == 0)
			{
				this.tbTopElevation.Text = this.GetElevationByLevelIndex(this.cmbTopLevel.SelectedIndex);
			}
			this.tbBottomElevation.Text = streamReader.ReadLine();
			if (this.tbBottomElevation.Text.CompareTo("") == 0)
			{
				this.tbBottomElevation.Text = this.GetElevationByLevelIndex(this.cmbBottomLevel.SelectedIndex);
			}
			this.CalculateStepHeight();
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return;
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "CreateFootstepForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.rdbInput.Checked);
			streamWriter.WriteLine(this.tbPlatformWidth.Text);
			streamWriter.WriteLine(this.nudStepCount.Value.ToString());
			streamWriter.WriteLine(this.tbStepWidth.Text);
			streamWriter.WriteLine(this.tbTopElevation.Text);
			streamWriter.WriteLine(this.tbBottomElevation.Text);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private ExternalCommandData m_Revit;

		private List<Level> m_Levels = new List<Level>();

		private List<double> m_LevelElevations = new List<double>();

		private List<Material> m_materials;

		private CreateFootstepForm.ResultType m_ResultType;

		private bool m_Initialized;

		public enum ResultType
		{
			RT_None,
			RT_Rect1,
			RT_Rect2,
			RT_Rect3,
			RT_Bow,
			RT_Freedom
		}
	}
}
